package com.xqboss.apps.service.box;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.constant.RedisKey;
import com.xqboss.apps.domain.box.BoxPriorityBuy;
import com.xqboss.apps.mapper.box.BoxPriorityBuyMapper;
import com.xqboss.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class BoxPriorityBuyService extends ServiceImpl<BoxPriorityBuyMapper, BoxPriorityBuy> {

    @Autowired
    private RedisCache redisCache;

    /**
     * 获取用户购买箱子时间
     *
     * @param userId
     * @param boxId
     * @return
     */
    public BoxPriorityBuy getBoxPriorityBuy(Long userId, Long boxId) {
        if(userId == null || boxId == null){
            return null;
        }
        String cacheKey = RedisKey.KEY_CACHE_BOX_PRIORITY_BUY_BY_BOX_USER + ":" + boxId + ":" + userId;
        BoxPriorityBuy boxPriorityBuy = redisCache.getCacheObject(cacheKey);
        if(boxPriorityBuy == null && !redisCache.hasKey(cacheKey)){
            boxPriorityBuy = getOne(new LambdaQueryWrapper<BoxPriorityBuy>()
                    .eq(BoxPriorityBuy::getUserId, userId)
                    .eq(BoxPriorityBuy::getBoxId, boxId));
            redisCache.setCacheObject(cacheKey, boxPriorityBuy, 3, TimeUnit.MINUTES);
        }
        return boxPriorityBuy;
    }

}
